//
// A sample calculator written in javascript
//

importPackage(org.eclipse.ui);
importPackage(org.eclipse.swt);
importPackage(org.eclipse.swt.widgets);
importPackage(org.eclipse.swt.events);
importPackage(org.eclipse.swt.layout);

c = new Composite(parent,SWT.NONE);
c.setLayout(new GridLayout(1,true));

t = new Text(c,SWT.BORDER);
t.layoutData = new GridData(GridData.FILL_HORIZONTAL);
t.editable = false;

buttonArea = new Composite(c,SWT.NONE);
buttonArea.setLayout(new GridLayout(4,true));
buttonArea.setLayoutData(new GridData(GridData.FILL_BOTH));

var labels = new Array(
"Clr","Bck","(",")",
"7","8","9","/",
"4","5","6","*",
"1","2","3","-",
"0",".","=","+");
 
keylistener = {   
  widgetSelected: function(event) { t.text += event.widget.text } ,
  widgetDefaultSelected: function(event) { }
}

evallistener = {
  widgetSelected: function(event) { t.text = eval("res = " + t.text)  } ,
  widgetDefaultSelected: function(event) { }
}

clearlistener = {
  widgetSelected: function(event) { t.text = ""  } ,
  widgetDefaultSelected: function(event) { }
}

backlistener = {
  widgetSelected: function(event) { 
	s = "" + t.text; // conversion to javascript string
	t.text = s.substring(0,s.length-1);
  },
  widgetDefaultSelected: function(event) { }
}

for (i = 0; i < labels.length; i++ ) {
	b = new Button(buttonArea, SWT.BORDER);
	b.layoutData = new GridData(GridData.FILL_BOTH);
	b.text = labels[i];
	
	switch(labels[i]) {

	case "Clr" :
		b.addSelectionListener(new SelectionListener(this.clearlistener));
		break;
	case "Bck" :
		b.addSelectionListener(new SelectionListener(this.backlistener));
		break;		
	case "=" :
		b.addSelectionListener(new SelectionListener(this.evallistener));
		break;
	default:
		b.addSelectionListener(new SelectionListener(this.keylistener));
	}

}
